require("pszenum")
local cardtool = require "cardtool"
local filelog = require "filelog"

local robot = {}

function robot:new(obj)
	local entity = obj or {}
	setmetatable(entity, self)
	self.__index = self
	return entity
end

local function findplayingplayer(seats, seat)
	local tmpidx = {}
	for _, v in ipairs(seats) do
		if (v.state >= ESeatState.SEAT_STATE_PLAYING) and (v.state < ESeatState.SEAT_STATE_FOLD) then
			if v.index ~= seat.index then
				table.insert(tmpidx, v.index)
			end
		end
	end
	if #tmpidx == 0 then return end	-- 正常情况下因该是不会没有的
	local index = tmpidx[math.random(#tmpidx)]
	-- filelog.warn("findplayingplayer ==============>", index)
	return index
end

function robot:call(table_data, seat)
	local cdiamonds = seat.diamond
	local seecards = seat.seecards > 0 and 2 or 1
	if cdiamonds < 2 * table_data.cur_bets * 2 then
		local index = findplayingplayer(table_data.seats, seat)
		if index and (table_data.robotdata.seats[seat.index].actioncount or 0) > 0 then
			return self:compare(index)
		else
			return self:fold(table_data, seat)
		end
	end
	if cdiamonds < table_data.cur_bets * seecards then
		return self:fold(table_data, seat)
	end
	return {
        EActionType.ACTION_TYPE_CALL,
        0,
    }
end

function robot:fold(table_data, seat)
	if seat.comparewin > 0 then
		local index = findplayingplayer(table_data.seats, seat)
		if index then
			return self:compare(index)
		else
			seat.comparewin = seat.comparewin - 1
			return self:call(table_data, seat)
		end
	end
	return {
        EActionType.ACTION_TYPE_FOLD,
        0,
    }
end

function robot:raise(table_data, seat, value)
	local cdiamonds = seat.diamond
	local seecards = seat.seecards > 0 and 2 or 1
	local ndiamonds = table_data.conf.base_score * value * seecards
	if cdiamonds >= ndiamonds then
		-- 如果剩下的钱不够最大值比牌数量，直接比牌
		if cdiamonds - ndiamonds < table_data.conf.base_score * 5 * seecards then
			local index = findplayingplayer(table_data.seats, seat)
			if index and (table_data.robotdata.seats[seat.index].actioncount or 0) > 0 then
				return self:compare(index)
			else
				return self:fold(table_data, seat)
			end
		end
	end
	return {
        EActionType.ACTION_TYPE_RAISE,
        value,
    }
end

-- note: 比牌必须至少一次跟注
-- 不做钱剩余数量管理，即不会因为钱不够而做弃牌处理，不然太亏，所以其他地方必须做好剩余钱管理
function robot:compare(index)
	return {
		EActionType.ACTION_TYPE_COMPARE,
		index,
	}
end

function robot:analyse(table_data, seat, inventory)
	local card_type = cardtool.getCardType(seat.cards)
	-- ("robot:analyse: card_type = ", card_type, seat.cards[3].card_value)
	if (inventory or 0) <= 0 then
		if seat.canwinall then
			return self:call(table_data, seat)
		else
			return self:fold(table_data, seat)
		end
	end

	if card_type == 2 then	--顺子
		if table_data.action_type == EActionType.ACTION_TYPE_RAISE then
			if (table_data.robotdata.seats[seat.index].actioncount or 0) > 3 then
				if math.random(100) <= (table_data.action_param or 1) * 20 then
					local index = findplayingplayer(table_data.seats, seat)
					if index then
						return self:compare(index)
					else
						return self:fold(table_data, seat)
					end
				else
					return self:call(table_data, seat)
				end
			end
		else
			if (table_data.robotdata.seats[seat.index].actioncount or 0) > 8 then
				local index = findplayingplayer(table_data.seats, seat)
				if index then
					return self:compare(index)
				else
					return self:fold(table_data, seat)
				end
			elseif (table_data.robotdata.seats[seat.index].actioncount or 0) > 4 then
				if math.random(100) <= 5 then
					return self:fold(table_data, seat)
				else
					local index = findplayingplayer(table_data.seats, seat)
					if index then
						return self:compare(index)
					else
						return self:fold(table_data, seat)
					end
				end
			end
		end
		return self:call(table_data, seat)
	elseif card_type == 1 then	--对子
		if table_data.action_type == EActionType.ACTION_TYPE_RAISE then
			if (table_data.robotdata.seats[seat.index].actioncount or 0) > 0 then
				if math.random(100) <= (table_data.action_param or 1) * 20 then
					local index = findplayingplayer(table_data.seats, seat)
					if index then
						return self:compare(index)
					else
						return self:fold(table_data, seat)
					end
				else
					return self:call(table_data, seat)
				end
			end
		else
			if (table_data.robotdata.seats[seat.index].actioncount or 0) > 6 then
				if math.random(100) <= 80 then
					local index = findplayingplayer(table_data.seats, seat)
					if index then
						return self:compare(index)
					else
						return self:fold(table_data, seat)
					end
				else
					return self:call(table_data, seat)
				end
			elseif (table_data.robotdata.seats[seat.index].actioncount or 0) > 3 then
				if (table_data.cur_bets or 0) == table_data.conf.base_score * 5 then
					local index = findplayingplayer(table_data.seats, seat)
					if index then
						return self:compare(index)
					else
						return self:fold(table_data, seat)
					end
				end

				if math.random(100) <= 60 then
					local index = findplayingplayer(table_data.seats, seat)
					if index then
						return self:compare(index)
					else
						return self:fold(table_data, seat)
					end
				else
					return self:call(table_data, seat)
				end
			end
		end
		return self:call(table_data, seat)
	elseif card_type == 0 then
		if seat.cards[3].card_value <= 10 then
			return self:fold(table_data, seat)
		elseif seat.cards[3].card_value < 13 then
			if math.random(100) <= 50 then
				if (table_data.robotdata.seats[seat.index].actioncount or 0) > 0 then
					local index = findplayingplayer(table_data.seats, seat)
					if index then
						return self:compare(index)
					else
						return self:fold(table_data, seat)
					end
				else
					return self:call(table_data, seat)
				end
			else
				return self:fold(table_data, seat)
			end
		end
		if math.random(100) <= 70 then
			if (table_data.robotdata.seats[seat.index].actioncount or 0) > 0 then
				local index = findplayingplayer(table_data.seats, seat)
				if index then
					return self:compare(index)
				else
					return self:fold(table_data, seat)
				end
			else
				return self:call(table_data, seat)
			end
		else
			return self:fold(table_data, seat)
		end
		return self:fold(table_data, seat)
	else
		if card_type == 3 then	--同花
			if seat.cards[3].card_value < 13 then
				if (table_data.robotdata.seats[seat.index].actioncount or 0) > 8 then
					if math.random(100) <= 50 then
						local index = findplayingplayer(table_data.seats, seat)
						if index then
							return self:compare(index)
						else
							return self:call(table_data, seat)
						end
					end
				end
				return self:call(table_data, seat)
			end
		end
		if math.random(100) <= 50 then
			local value = table_data.robotdata.raisevalue or 1
			if table_data.robotdata.raisevalue < 5 then
				value = value + math.random(5 - table_data.robotdata.raisevalue)
			end
			local canraise = true
			if seat.seecards > 0 then
				if seat.diamond < table_data.conf.base_score * value * 2 then
					canraise = false
				end
			else
				if seat.diamond < table_data.conf.base_score * value then
					canraise = false
				end
			end
			if canraise then
				return self:raise(table_data, seat, value)
			else
				return self:call(table_data, seat)
			end
		else
			return self:call(table_data, seat)
		end
	end
end

return robot
